package com.simple.demo.socket;

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import jdk.internal.util.xml.impl.Input;

import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;

public class Client {

    public static void main(String[] args) {

        try {
            Socket socket = new Socket("localhost", 5000);
            OutputStream out = socket.getOutputStream();
            out.write("我是客户端".getBytes());
            out.flush();
            //读取服务器端发送过来的消息
            InputStream input = socket.getInputStream();
            byte [] readByte = new byte[4];
            int readLen = 0;
            ByteBuffer buffer = ByteBuffer.allocate(100);
            while((readLen = input.read(readByte)) > 0){
                buffer.put(readByte,0,readLen);
            }
            buffer.flip();
            System.out.println(new String(buffer.array()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
